package gwin.com.numbers;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private GameView gameView;
    private Chronometer timer;
    private ImageView leftKey;
    private Button start;
    private Button setting;
    private View startView;
    private ImageView result;
    private ImageView centerKey;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gameView = findViewById(R.id.game_view);
        leftKey = findViewById(R.id.left_key);
        centerKey = findViewById(R.id.center_key);
        result = findViewById(R.id.result);
        startView = findViewById(R.id.start_view);
        start = findViewById(R.id.start);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start();
            }
        });
        setting = findViewById(R.id.setting);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setting();
            }
        });
        timer = findViewById(R.id.timer);
        gameView.setGameListener(new Game.GameListener() {
            @Override
            public void onGameOver(boolean isSuccess) {
                timer.stop();
                leftKey.setImageResource(R.drawable.ic_setting);
                centerKey.setImageResource(R.drawable.ic_restore);
                result.setVisibility(View.VISIBLE);
                result.setImageResource(isSuccess ? R.drawable.ic_success : R.drawable.ic_fail);
            }

            @Override
            public void onGameStart() {
                timer.setBase(SystemClock.elapsedRealtime());
                timer.start();
                leftKey.setImageResource(R.drawable.ic_clear);
                centerKey.setImageResource(R.drawable.ic_yes);
                centerKey.setVisibility(View.VISIBLE);
                result.setVisibility(View.INVISIBLE);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            setting();
            return true;
        }else if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
            Log.e("zui","OnClickListener2");
            start();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void start() {
        startView.setVisibility(View.GONE);
        gameView.setVisibility(View.VISIBLE);
        leftKey.setVisibility(View.VISIBLE);
        leftKey.setImageResource(R.drawable.ic_flag);
        gameView.game.init();
        gameView.requestFocus();
        gameView.invalidate();

        if (gameView.game.gameListener != null) {
            gameView.game.gameListener.onGameStart();
        }
    }

    private void setting() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setItems(new String[]{
                getString(R.string.easy),
                getString(R.string.normal),
                getString(R.string.hard),
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        gameView.game.fact = gameView.game.EASY;
                        break;
                    case 1:
                        gameView.game.fact = gameView.game.NORMAL;
                        break;
                    case 2:
                        gameView.game.fact = gameView.game.HARD;
                        break;
                }
            }
        }).create();
        dialog.show();
    }

}
